package troisdimension.client;

import java.util.ArrayList;

import com.google.gwt.canvas.dom.client.CanvasPixelArray;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.ImageData;
import com.google.gwt.user.client.Random;

public class Forme {
	protected String id = null;
	protected Point pt = null;
	protected PointCentral ptCentral = null;
	protected transformation transforme = new transformation();
	protected String couleur = "000";
	public String getCouleur() {
		return couleur;
	}

	public void setCouleur(String couleur) {
		this.couleur = couleur;
	}
	
	public transformation getTransforme() {
		return transforme;
	}

	public Point getPt() {
		return pt;
	}

	public void setPt(Point pt) {
		this.pt = pt;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getString() {
		return this.id;
	}

	public PointCentral getPtCentral() {
		return ptCentral;
	}

	public void setPtCentral(PointCentral ptCentral) {
		this.ptCentral = ptCentral;
	}

	private boolean isInRectangle(ArrayList<Point> points, double x, double y) {
		int i = 0;
		int j = 0;
		int xtoInt = (int) x;
		int ytoInt = (int) y;
		boolean res = false;
		for (i = 0, j = points.size() - 1; i < points.size(); j = i++) {
			Point pti = points.get(i);
			Point ptj = points.get(j);

			if ((((pti.getY2d() <= ytoInt) && (ytoInt < ptj.getY2d())) || ((ptj
					.getY2d() <= ytoInt) && (y < pti.getY2d())))
					&& (xtoInt < (ptj.getX2d() - pti.getX2d())
							* (ytoInt - pti.getY2d())
							/ (ptj.getY2d() - pti.getY2d()) + pti.getX2d())) {
				res = !res;
			}
		}
		return res;
	}

	public boolean PointInPolygon(ArrayList<Point> points, double x, double y) {
		int r = points.size() - 1;
		boolean oddNodes = false;
		int xtoInt = (int) x;
		int ytoInt = (int) y;
		for (int t = 0; t < points.size(); t++) {
			double prx = points.get(r).getX2d();
			double pry = points.get(r).getY2d();
			double ptx = points.get(t).getX2d();
			double pty = points.get(t).getY2d();

			if (pty < ytoInt && pty >= ytoInt || pry < ytoInt && pty >= ytoInt) {
				if (ptx + (ytoInt - pty) / (pry - pty) * (prx - ptx) < xtoInt) {
					oddNodes = !oddNodes;
				}
			}
			r = t;
		}

		return oddNodes;
	}

	private ArrayList<Point> listePositionDansLigne(Context2d ctx,
			ArrayList<Point> pts, Point p1, Point p2) {
		Point pFinal = null;
		double i = 0;
		do {

			pFinal = positionDansLigne(p1, p2, i);
			pts.add(pFinal);

			ctx.setLineWidth(5);
			ctx.beginPath();
			ctx.arc(pFinal.getX2d(), pFinal.getY2d(), 1, 0, 360);
			ctx.stroke();
			ctx.closePath();
			ctx.save();

			i = i + 0.1;

		} while ((int) i != 1);

		return pts;
	}

	private ArrayList<Point> listePositionDansLigne(Context2d ctx,
			ArrayList<Point> pts, Point p1, Point p2, Point p3, Point p4) {
		Point pFinal = null;
		double i = 0;
		do {

			pFinal = positionDansBezier(p1, p2, p3, p4, i);
			pts.add(pFinal);

			ctx.setLineWidth(5);
			ctx.beginPath();
			ctx.arc(pFinal.getX2d(), pFinal.getY2d(), 1, 0, 360);
			ctx.stroke();
			ctx.closePath();
			ctx.save();

			i = i + 0.1;

		} while ((int) i != 1);

		return pts;
	}

	private boolean isMemePoint(Point p1, Point p2) {
		int x1 = (int) p1.getX2d();
		int x2 = (int) p2.getX2d();
		int y1 = (int) p1.getY2d();
		int y2 = (int) p2.getY2d();
		return x1 == x2 && y1 == y2;
	}

	private Point positionDansLigne(Point p1, Point p2, double pos) {
		Point pRes = new Point();
		pRes.setX2d((1 - pos) * p1.getX2d() + pos * p2.getX2d());
		pRes.setY2d((1 - pos) * p1.getY2d() + pos * p2.getY2d());
		return pRes;
	}

	private Point positionDansBezier(Point p1, Point p2, Point p3, Point p4,
			double pos) {
		Point pRes = new Point();
		double s = 1 - pos;
		double c1 = s * s * s;
		double c2 = s * s;
		double c3 = pos * pos;
		double c4 = pos * pos * pos;
		pRes.setX2d(c1 * p1.getX2d() + 3 * c2 * pos * p2.getX2d() + 3 * s * c3
				* p3.getX2d() + c4 * p4.getX2d());
		pRes.setY2d(c1 * p1.getY2d() + 3 * c2 * pos * p2.getY2d() + 3 * s * c3
				* p3.getY2d() + c4 * p4.getY2d());
		return pRes;
	}

	public void dessine(Context2d ctx, ArrayList<Point> listePoint) {

		int xmin = 10;
		int xmax = 400;
		int ymin = 50;
		int ymax = 300;
		ImageData imgDt = ctx.getImageData(0, 0, 1000, 600);
		CanvasPixelArray pix = imgDt.getData();
		for (int i = xmin; i <= xmax; i++) {
			for (int j = ymin; j <= ymax; j++) {
				if (isInRectangle(listePoint, i, j)) {
					int p = (i * 4) + (j * 4000);
					pix.set(p, Random.nextInt(25));
					pix.set(p + 1, Random.nextInt(25));
					pix.set(p + 2, Random.nextInt(25));
				}
			}
		}

		ctx.putImageData(imgDt, 0, 0);

	}

	public void dessiner(Context2d ctx){
		
	}
	
	public void apparence(Context2d ctx, boolean isMove){

	}
	
}
